import { createRouter, createWebHashHistory } from 'vue-router'

import Home from "../views/home.vue";
import PointMarket from "../views/pointMarket.vue";
import NeighborMarket from "../views/neighborMarket.vue";
import Message from "../views/message.vue";
import Chat from "../views/chat.vue"; // 新增聊天页面
import Profile from "../views/profile.vue";
import ForgotPassword from "../views/forgotPassword.vue";

import Login from "../views/login.vue";
import Start from "../views/start.vue";
import Register from "../views/register.vue";
import PointMarketDetail from "../views/pointMarketDetail.vue";
import MyPoints from "../views/myPoints.vue";
import PointsPlan from "../views/pointsPlan.vue";


import PostDetail from "../views/postDetail.vue";
import {localGet} from "../utils";
import Search from "../views/search.vue";
import Notifications from "../views/notifications.vue";
import NotifycationDetail from "../views/notificationDetail.vue";
import startdiscussion from "../views/startdiscussion.vue";
import starthelp from "../views/starthelp.vue";
import startsell from "../views/startsell.vue";
import Fab from "../views/fab.vue";
import PayCancel from "../components/PayCancel.vue";
import PayCanceled from "../components/PayCanceled.vue";
import myPoints from "../views/myPoints.vue";
import MyItems from "../views/myItems.vue";
import MyItemDetail from "../views/myItemDetail.vue";
import MyOrders from "../views/MyOrders.vue";
import MyOrderDetail from "../views/myOrderDetail.vue";
import MyCollections from "../views/myCollections.vue";

const router = createRouter({
    history: createWebHashHistory(),
    routes : [
        {
            path: '/',
            redirect: '/start'
        },
        {
            path: '/start',
            name: 'start',
            component: Start,
            meta: { hideTabBar: true, hideHeader: true },
            children: [
                {
                    path: '', // 默认子路由，显示登录或注册导航
                    component: Login
                },
                {
                    path: 'login',
                    name: 'login',
                    component: Login,
                    meta: { hideTabBar: true, hideHeader: true }
                },
                {
                    path: 'register',
                    name: 'register',
                    component: Register,
                    meta: { hideTabBar: true, hideHeader: true }
                },
                {
                    path: 'forgotPassword',
                    name: 'forgotPassword',
                    component: ForgotPassword,
                    meta: { hideTabBar: true, hideHeader: true }
                }
            ]
        },
        {
            path: '/home',
            name: 'home',
            component: Home,
            // 这个页面会显示 TabBar 和 Header
            meta: { hideTabBar: false,keepAlive:true }
        },
        {
            path: '/pointMarket',
            name: 'pointMarket',
            component: PointMarket,
            // 为邻里集市页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: false, hideHeader: true }
        },
        {
            path: '/neighborMarket',
            name: 'neighborMarket',
            component: NeighborMarket,
            // 为邻里集市页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/message',
            name: 'message',
            component: Message,
            // 好友列表页面显示导航
            meta: { hideTabBar: false, hideHeader: false }
        },
        {
            path: '/chat/:friendId', // 新增聊天页面路由
            name: 'chat',
            component: Chat,
            // 聊天页面隐藏导航
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/profile',
            name: 'profile',
            component: Profile,
            meta: { hideTabBar: false, hideHeader: false }
        },
        {
            path: '/search',
            name: 'search',
            component: Search,
            // 为邻里集市页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: false, hideHeader: false }
        },
        {
            path: '/fab',
            name: 'fab',
            component: Fab,
            // 为邻里集市页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/PayCancel', // 取消支付页路由
            name: 'PayCancel',
            component: PayCancel,
            meta: { title: '取消支付',hideTabBar: true, hideHeader: true }
        },
        {
            path: '/PayCanceled', // 支付已取消页路由
            name: 'PayCanceled',
            component: PayCanceled,
            meta: { title: '支付已取消',hideTabBar: true, hideHeader: true }
        },
 {
            path: '/pointMarketDetail',
            name: 'pointMarketDetail',
            component: PointMarketDetail,
            // 为邻里集市页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myPoints',
            name: 'myPoints',
            component: MyPoints,
            // 为邻里集市页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/pointsPlan',
            name: 'pointsPlan',
            component: PointsPlan,
            // 为邻里集市页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/post/:id',
            name: 'postDetail',
            component: PostDetail,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: false }
        },
        {
            path: '/notifications',
            name: 'notifications',
            component: Notifications,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: false }
        },
        {
            path: '/notificationDetail',
            name: 'notificationDetail',
            component: NotifycationDetail,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: false }
        },
        {
            path: '/startdiscussion',
            name: 'startdiscussion',
            component: startdiscussion,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/starthelp',
            name: 'starthelp',
            component: starthelp,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/startsell/:type?',
            name: 'startsell',
            component: startsell,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myItems',
            name: 'myItems',
            component: MyItems,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myOrderDetail/:id',
            name: 'myOrderDetail',
            component: MyOrderDetail,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myOrders',
            name: 'myOrders',
            component: MyOrders,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/fab',
            name: 'fab',
            component: Fab,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myPoints',
            name: 'myPoints',
            component: myPoints,
            // 为邻帖子详情页面添加元信息，告诉主布局组件隐藏通用组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myItems',
            name: 'myItems',
            component: MyItems, // 需要导入对应的组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myItemDetail/:id',
            name: 'myItemDetail',
            component: MyItemDetail, // 需要导入对应的组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myOrders',
            name: 'myOrders',
            component: MyOrders, // 需要导入对应的组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myOrderDetail/:id',
            name: 'myOrderDetail',
            component: MyOrderDetail, // 需要导入对应的组件
            meta: { hideTabBar: true, hideHeader: true }
        },
        {
            path: '/myCollections',
            name: 'myCollections',
            component: MyCollections, // 需要导入对应的组件
            meta: { hideTabBar: true, hideHeader: true }
        },

    ]
});

router.beforeEach((to, from ,next) => {
    const whiteList = ["/start","/start/register","/start/forgotpassword","/start/login"] ;//白名单地址
    console.log(to.path);
    const token = localGet("token");
    if(token || whiteList.includes(to.path)){
        console.log("next")
        next();
    }
    else{
        next("/start");
    }
})

export default router